package com.bhs.sansonglogistics.ui.wallet;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.ArrayMap;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.base.Constants;
import com.bhs.sansonglogistics.bean.AdvancePayment;
import com.bhs.sansonglogistics.bean.PayInfo;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.utils.LogUtils;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bhs.sansonglogistics.utils.PayUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.google.gson.Gson;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import net.security.device.api.LogUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 充值
 */
public class TopUpActivity extends BaseActivity implements NetCallBack, View.OnClickListener {

    private TextView mTvBill;
    private EditText mEtMoney;
    private TextView mTvTopUpType;
    private Button mBtnSubmit;
    private IWXAPI msgApi;

    @Override
    public int initLayout() {
        return R.layout.activity_top_up;
    }

    @Override
    public void initView() {
        //注册监听
        EventBus.getDefault().register(this);
        mTvBill = findViewById(R.id.tv_bill);
        mEtMoney = findViewById(R.id.et_money);
        findViewById(R.id.ll_top_up_type).setOnClickListener(this);
        mTvTopUpType = findViewById(R.id.tv_top_up_type);
        mBtnSubmit = findViewById(R.id.btn_submit);
        mBtnSubmit.setOnClickListener(this);
        msgApi = WXAPIFactory.createWXAPI(mActivity, null);
        // 将该app注册到微信
        msgApi.registerApp(Constants.APP_ID);
    }


    @Override
    public void initData() {
        mEtMoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                verify();
            }
        });
    }

    private void verify() {
        boolean verify = !TextUtils.isEmpty(mEtMoney.getText());
        mBtnSubmit.setEnabled(verify);
        mBtnSubmit.setBackgroundColor(verify ? Color.parseColor("#485EF4") : Color.parseColor("#7F485EF4"));
    }

    @Subscribe
    public void onEvent(BaseResp event) {
        if (event.errCode == BaseResp.ErrCode.ERR_OK) {
            finish();
        }
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        if (requestCode == 0) {
            AdvancePayment payment = new Gson().fromJson(json, AdvancePayment.class);
            if (payment.isStatus()) {
                Map<String, String> map = new ArrayMap<>();
                map.put("pay_sn", payment.getData().getPay_sn());
                map.put("pay_type", "4");//支付方式 4微信支付
                map.put("requet_type", "app");
                map.put("platform", "logistics");
                networkRequest(netApi.go_pay(map), this, 888);
            } else {
                ToastUtil.show(payment.getMsg());
            }
        } else if (requestCode == 888) {
            PayInfo payment = new Gson().fromJson(json, PayInfo.class);
            if (payment.isStatus()) {
                PayUtils.weChatPay(mContext, payment.getData().getPay_params(), msgApi);
            } else {
                ToastUtil.show(payment.getMsg());
            }
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_submit) {
            networkRequest(netApi.apply_recharge(mEtMoney.getText().toString()), this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}